import axios from 'axios'
import { MessageBox, Message,Notification } from 'element-ui'
import store from '@/store'
import { ACCESS_TOKEN } from "@/store/mutation-types"
import Vue from 'vue'

let baseDomain = window._CONFIG['domianURL'];
let baseProject = baseDomain.substring(baseDomain.lastIndexOf("/"));

// create an axios instance
const service = axios.create({
  baseURL: baseProject,
  timeout: 1000 * 10
})

const err = (error) => {
  if (error.response) {
    let data = error.response.data
    const token = Vue.ls.get(ACCESS_TOKEN)

    switch (error.response.status) {
      case 403:
        Notification.error({ message: '拒绝访问', description: '',duration: 4})
        break
      case 500:
        //notification.error({ message: '系统提示', description:'Token失效，请重新登录!',duration: 4})
        if(token && data.message=="Token失效，请重新登录"){
          // update-begin- --- author:scott ------ date:20190225 ---- for:Token失效采用弹框模式，不直接跳转----
          // store.dispatch('Logout').then(() => {
          //     window.location.reload()
          // })
          MessageBox.confirm('很抱歉，登录已过期，请重新登录?', '登录已过期', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            store.dispatch('Logout').then(() => {
              location.reload()
            })
          })
          // update-end- --- author:scott ------ date:20190225 ---- for:Token失效采用弹框模式，不直接跳转----
        }
        break
      case 404:
          Notification.error({ message: '很抱歉，资源未找到!', description:''})
        break
      case 504:
          Notification.error({ message: '网络超时', description: ''})
        break;
      case 401:
          Notification.error({ message: '未授权，请重新登录', description:''})
        if (token) {
          store.dispatch('Logout').then(() => {
            setTimeout(() => {
            window.location.reload()
          }, 1500)
        })
        }
        break
      default:
        Notification.error({
          title: '系统提示',
          message: data.message
        });
        break
    }
  }
  console.error(error);
  return Promise.reject(error)
};

// request interceptor
service.interceptors.request.use(
  config => {
    // do something before request is sent
    const token = Vue.ls.get(ACCESS_TOKEN)
    if (token) {
      // let each request carry token
      // ['X-Token'] is a custom headers key
      // please modify it according to the actual situation
      config.headers['X-Access-Token'] = token
    }
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
  */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  response => response.data,
  err
)

export default service
